<template>
	<view class="content">
		<view>
			<u-upload
				:fileList="fileList1"
				@afterRead="afterRead"
				@delete="deletePic"
				name="1"
				multiple
				:maxCount="10"
			>
				</u-upload>
		</view>
		<view class="title">
			  <u--input
			    placeholder="请输入内容"
			    border="surround"
			    v-model="title"
			  ></u--input>
		</view>
		<view>
			<u-textarea v-model="content" placeholder="请输入内容" ></u-textarea>
		</view>
		<button class="goSend" @click="goSend">发布笔记</button>
	</view>
</template>

<script>
	import baseInfo from '@/config/base.js';
	import http from '@/apis/http.js'
	console.log()
	export default {
		data() {
			return {
				fileList1:[],
				title:'',
				content:'',
			}
		},
		methods: {
				// 删除图片
						deletePic(event) {
							this[`fileList${event.name}`].splice(event.index, 1)
						},
						// 新增图片
						async afterRead(event) {
							// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
							let lists = [].concat(event.file)
							let fileListLen = this[`fileList${event.name}`].length
							lists.map((item) => {
								this[`fileList${event.name}`].push({
									...item,
									status: 'uploading',
									message: '上传中'
								})
							})
							for (let i = 0; i < lists.length; i++) {
								const result = await this.uploadFilePromise(lists[i].url)
								let item = this[`fileList${event.name}`][fileListLen]
								this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
									status: 'success',
									message: '',
									url: result
								}))
								fileListLen++
							}
						},
						
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseInfo.baseUrl+'index.php/api/tourism_life/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							res = JSON.parse(res.data)
							setTimeout(() => {
								resolve(res.data.url)
							}, 1000)
						}
					});
				})
			},
			
			goSend(){
				let images = '';
				for(var i in this.fileList1){
					images += this.fileList1[i].url + ',';
				}
				http.POST('tourism_life/send',{
					'images':images,
					'title' : this.title,
					'content':this.content
				}).then(res=>{
					if(res.code == 1){
						uni.showToast({
							'title':'添加成功',
							'icon':'none'
						})
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/tourismLife/tourismLife'
							})
						},1000)
					}else{
						uni.showToast({
							'title':res.msg,
							'icon':'none'
						})
					}
				})
				
			}
		}
	}
</script>

<style>
	.title{margin: 20rpx 0;}
	.content{padding: 0 20rpx;}
	.goSend {
		position: fixed;
		width: 90%;
		left: 5%;
		height: 80rpx;
		line-height: 80rpx;
		color: #fff;
		background-color: red;
		border-radius: 50rpx;
		bottom: 30rpx;
	}
</style>
